// page/Receive_address/index5.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        myAddress: '我的收货地址',
        normal: '默认',
        personalName: '小白兔不吃胡萝卜',
        personalNum: '15082898888',
        personalAddress: '重庆 重庆市 巴南区 龙洲湾街道 重庆财经学院',
        normal_address: '设为默认地址',
        check: true,
        addressList: [],
        address: [],
        addressId:0,
        receiver:'',
        phone:0,
        city:"",
        area:''
    },

    getAddressList() {
        wx.get('http://localhost:3000/wx/address', ).then(res => {
            this.setData({
                addressList: res
            })
        })
    },

    radiocon: function (e) {
        let index = e.currentTarget.dataset.index
        let address = this.data.addressList[index]
        console.log("adddd",address);
        wx.put('http://localhost:3000/wx/address/default', {
            addressId: address._id,
            memberId: wx.userInfo._id,
            isDefault: address.isDefault == 1 ? 0 : 1
        }).then(res => {
            if (!res.errorMsg) {
                this.getAddressList()
            }
        })
    },

    delete(e) { //删除地址
        console.log("e",e);
        let index = e.currentTarget.dataset.index
        let address = this.data.addressList[index]
        console.log("add",address);
        wx.showModal({
            title: '删除提示',
            content: '确认删除该地址？',
            success:res => {
              if (res.confirm) {//确定删除
                wx.DELETE('http://localhost:3000/wx/address',{
                    addressId:address._id
                }).then(res => {
                    if(!res.errorMsg){
                        this.getAddressList()//刷新列表
                    }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
    },

    addressChange(e){//修改地址
        let index = e.currentTarget.dataset.index
        let address = this.data.addressList[index]
        console.log("add",address._id);
        wx.navigateTo({
          url:"/page/receive_object_add/index4?addressId="+address._id,
        })
    },

    getAddressDetail(){
        wx.get('http://localhost:3000/wx/address/datail',{
            addressId:this.data.addressId
        }).then(res=>{
            console.log("detail",res);
            this.setData({
                receiver:res.receiver,
                phone:res.phone,
                city:res.city,
                area:res.city
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.getAddressList()
        if(options.addressId){
            this.setData({
                addressId:options.addressId
            })
            this.getAddressDetail()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.getAddressList()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})